/*
 * Filename:    IndicationReportExportService.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月28日
 */
package com.paic.mhis.hcpms.quota.settlement.biz.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.quota.settlement.biz.service.IndicationReportExportService;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationBeanDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgDTO;

@Component("indicationReportExportServiceImpl")
public class IndicationReportExportServiceImpl implements IndicationReportExportService {
	private static final Logger logger = Logger.getLogger(IndicationReportExportServiceImpl.class);
	public static final String[] str = { "名称", "7月", "8月", "9月", "10月", "11月", "12月", "1月", "2月", "3月", "4月", "5月",
			"6月", "同比", "环比" };

	public void exportRegionExcel(String excelName, List<IndicationBeanDTO> list, HttpServletResponse response)
			throws IOException {
		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			for (String key : str) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容.
			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					IndicationBeanDTO dto = list.get(i);
					row = sheet.createRow(i + 1);
					row.createCell(0).setCellValue(dto.getName());
					row.createCell(1).setCellValue(dto.getSeventh());
					row.createCell(2).setCellValue(dto.getEighth());
					row.createCell(3).setCellValue(dto.getNinth());
					row.createCell(4).setCellValue(dto.getTenth());
					row.createCell(5).setCellValue(dto.getEleventh());
					row.createCell(6).setCellValue(dto.getTwelfth());
					row.createCell(7).setCellValue(dto.getFirst());
					row.createCell(8).setCellValue(dto.getSecond());
					row.createCell(9).setCellValue(dto.getThird());
					row.createCell(10).setCellValue(dto.getFourth());
					row.createCell(11).setCellValue(dto.getFifth());
					row.createCell(12).setCellValue(dto.getSixth());
					row.createCell(13).setCellValue(dto.getYoy());
					row.createCell(14).setCellValue(dto.getMom());

				}
			}
			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			logger.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();
		}
	}

	private void initResponse(HttpServletResponse response, String name) {
		try {
			response.addHeader("Pragma", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
			response.reset();
			response.setContentType("APPLICATION/OCTET-STREAM");
			String fileName = new String((name).getBytes("utf-8"), "ISO8859-1") + ".xls";
			response.setHeader("content-disposition", "attachment;fileName=\"" + fileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void exportLevelOrgExcel(String excelName, List<IndicationRegionOrgDTO> list, HttpServletResponse response)
			throws IOException {
		Collections.sort(list);
		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			for (String key : str) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容
			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					IndicationRegionOrgDTO dto = (IndicationRegionOrgDTO) list.get(i);
					row = sheet.createRow(i + 1);
					row.createCell(0).setCellValue(dto.getName());
					row.createCell(1).setCellValue(dto.getSeventh());
					row.createCell(2).setCellValue(dto.getEighth());
					row.createCell(3).setCellValue(dto.getNinth());
					row.createCell(4).setCellValue(dto.getTenth());
					row.createCell(5).setCellValue(dto.getEleventh());
					row.createCell(6).setCellValue(dto.getTwelfth());
					row.createCell(7).setCellValue(dto.getFirst());
					row.createCell(8).setCellValue(dto.getSecond());
					row.createCell(9).setCellValue(dto.getThird());
					row.createCell(10).setCellValue(dto.getFourth());
					row.createCell(11).setCellValue(dto.getFifth());
					row.createCell(12).setCellValue(dto.getSixth());
					row.createCell(13).setCellValue(dto.getYoy());
					row.createCell(14).setCellValue(dto.getMom());

				}
			}
			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			logger.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();
		}
	}

}
